<template>
  <a-modal
    v-model:visible="props.visible"
    :title="props.${entity?lower_case}Id ? '编辑' : '新增'"
    width="500px"
    @cancel="dialogClose"
  >
    <a-form
      class="ls-form"
      ref="formRef"
      :model="formData"
      :label-col="{ style: { width: '85px' } }"
    >
  <#list table.fields as field>
    <#if field.propertyName != 'id' 
      && field.propertyName != 'createUser' 
      && field.propertyName != 'createTime' 
      && field.propertyName != 'updateUser' 
      && field.propertyName != 'updateTime' 
      && field.propertyName != 'delFlag' 
      && field.propertyName != 'tenantId'
    >
    <#if (field.customMap?? && field.customMap?size > 0)>
      <#if (field.customMap?size > 2)>
      <a-form-item
        label="${field.comment}"
        name="${field.propertyName}"
        :rules="{ required: true, message: '请选择${field.comment}', trigger: 'change' }"
      >
        <a-select v-model:value="formData.${field.propertyName}" placeholder="请选择${field.comment}">
        <#list field.customMap?keys as key>
          <a-select-option :value="${key}">${field.customMap[key]!}</a-select-option>
        </#list>
        </a-select>
      </a-form-item>
      <#else>
      <a-form-item label="${field.comment}" name="${field.propertyName}">
        <a-radio-group v-model:value="formData.${field.propertyName}" name="${field.propertyName}">
        <#list field.customMap?keys as key>
          <a-radio :value="${key}">${field.customMap[key]!}</a-radio>
        </#list>
        </a-radio-group>
      </a-form-item>
      </#if>
    <#elseif field.propertyName == 'cover' || field.propertyName == 'logo' || field.propertyName == 'image'>
      <a-form-item
        label="${field.comment}"
        name="${field.propertyName}"
        :rules="{ required: true, message: '请上传${field.comment}', trigger: 'change' }"
      >
        <UploadImg
          @change-file-name="(name) => (formData.imageImgName = name)"
          :fileType="['image/jpeg', 'image/png', 'image/jpg', 'image/gif']"
          name="${entity?lower_case}"
          :fileSize="200"
          v-model:image-url="formData.${field.propertyName}"
          :cropper="false"
        >
          <template #tip>支持扩展名: jpg png jpeg;文件大小不超过200M</template>
        </UploadImg>
      </a-form-item>
    <#elseif field.columnType?string == 'INTEGER'>
      <a-form-item label="${field.comment}" name="${field.propertyName}">
        <a-input-number v-model:value="formData.${field.propertyName}" />
      </a-form-item>
    <#elseif field.columnType?string == 'LOCAL_DATE_TIME'>
      <a-form-item
        label="${field.comment}"
        name="${field.propertyName}"
        :rules="{ required: true, message: '请选择${field.comment}', trigger: 'change' }"
      >
        <a-date-picker
          show-time
          placeholder="请选择${field.comment}"
          v-model:value="formData.${field.propertyName}"
          format="YYYY-MM-DD HH:mm:ss"
          valueFormat="YYYY-MM-DD HH:mm:ss"
        />
      </a-form-item>
    <#elseif field.columnType?string == 'LOCAL_DATE'>
      <a-form-item
        label="${field.comment}"
        name="${field.propertyName}"
        :rules="{ required: true, message: '请选择${field.comment}', trigger: 'change' }"
      >
        <a-date-picker
          show-time
          placeholder="请选择${field.comment}"
          v-model:value="formData.${field.propertyName}"
          format="YYYY-MM-DD"
          valueFormat="YYYY-MM-DD"
        />
      </a-form-item>
    <#else>
      <a-form-item
        label="${field.comment}"
        name="${field.propertyName}"
        :rules="{ required: true, message: '请输入${field.comment}', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.${field.propertyName}" placeholder="请输入${field.comment}" allow-clear />
      </a-form-item>
    </#if>
    </#if>
  </#list>
    </a-form>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
        <a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
      </span>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import type { FormInstance } from 'ant-design-vue';
  import { get${entity}Detail, ${entity?lower_case}Add, ${entity?lower_case}Update } from '@/api/tool/${entity?lower_case}';
  import UploadImg from '@/components/Upload/Image.vue';
  import { onMounted, reactive, shallowRef } from 'vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';

  /**
   * 定义参数变量
   */
  const emit = defineEmits(['success', 'update:visible']);
  const formRef = shallowRef<FormInstance>();

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
  <#list table.fields as field>
    <#if field.propertyName != 'id' 
      && field.propertyName != 'createUser' 
      && field.propertyName != 'createTime' 
      && field.propertyName != 'updateUser' 
      && field.propertyName != 'updateTime' 
      && field.propertyName != 'delFlag' 
      && field.propertyName != 'tenantId'
    >
    <#if (field.customMap?? && field.customMap?size > 0)>
    ${field.propertyName}: ${field.metaInfo.defaultValue},
    <#elseif field.columnType?string == 'INTEGER'>
    ${field.propertyName}: ${field.metaInfo.defaultValue!0},
    <#else>
    ${field.propertyName}: '',
    </#if>
    </#if>
  </#list>
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    ${entity?lower_case}Id: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    await formRef.value?.validate();
    props.${entity?lower_case}Id ? await ${entity?lower_case}Update(formData) : await ${entity?lower_case}Add(formData);
    message.success('操作成功');
    emit('update:visible', false);
    emit('success');
  };

  /**
   * 关闭窗体
   */
  const dialogClose = () => {
    emit('update:visible', false);
  };

  const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await get${entity}Detail(props.${entity?lower_case}Id);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.${entity?lower_case}Id) {
      setFormData();
    }
  });
</script>
